package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.PhoneUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.PageBaseBean;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 意见反馈
 * 
 * @author li.li
 * 
 */
public class AccountFeedbackFragment extends BaseProgressFragment {

	@Bind(R.id.id_account_feedback_text)
	protected EditText feedbackText;
	@Bind(R.id.id_version_tv)
	protected TextView mTvVersion;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_feedback;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
		mTvVersion.setText(getResources().getString(R.string.app_name) + " V"
				+ PhoneUtils.getAppVersionName(caller));
	}

	@OnClick(R.id.id_account_feedback_btn)
	public void submit() {
		String feedback = feedbackText.getText().toString();

		if (StringUtils.isBlank(feedback)) {
			ViewUtils.toast(R.string.tip_feedback_empty);
			return;
		}

		// 网络请求
		AccessTokenKeeper token = AccessTokenKeeper
				.readAccessToken(getActivity());

		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("content", feedback);

			ApiRequest.accountFeedback.request(this, params);

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}
	}

	@Override
	public void onRequestResonse(PageBaseBean page) {
		if (isAdded())
			caller.finish();
	}

	public static void go(Activity act) {

		MyPageHelper.accountFeedback.showMyPage(act);
	}

}
